package com.jk.util;

import java.util.Random;

/**
 * Created by yanghuan on 2015/7/20.
 */
public class RandomStringUtil {

    public static String baseString = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    public static String baseNum = "0123456789";

    public static String getRandomString() {
        return getRandomString(20, 30);
    }

    /**
     * 举例  生成20-30长度的随机字符串
     *
     * @param min 随机长度开始 20
     * @param max 随机长度结束 30
     * @return
     */
    public static String getRandomString(int min, int max) {

        Random random = new Random();
        int times = random.nextInt(max) % (max - min + 1) + min;
        return getResult(times);

    }

    private static String getResult(int times) {

        StringBuffer result = new StringBuffer();
        for (int i = 0; i < times; i++) {
            Random random = new Random();
            int s = random.nextInt(baseString.length() - 1);
            result.append(baseString.charAt(s));
        }
        return result.toString();
    }

    private static String getResultNum(int times) {

        StringBuffer result = new StringBuffer();
        for (int i = 0; i < times; i++) {
            Random random = new Random();
            int s = random.nextInt(baseNum.length() - 1);
            result.append(baseNum.charAt(s));
        }
        return result.toString();
    }

    public static String getInvitationCode() {
        return getResultNum(6);
    }

    public static String getCaptcha(){
        return getResultCaptcha(6);
    }

    private  static String getResultCaptcha(int times){
        StringBuffer result = new StringBuffer();
        for(int i=0;i<times;i++){
            Random random = new Random();
            int s = random.nextInt(baseNum.length()-1);
            result.append(baseNum.charAt(s));
        }
        return result.toString();
    }

}
